<template>
  <!-- 头部查询、新增栏 -->
  <el-form :inline="true" class="demo-form-inline">
    <el-form-item>
      <el-input v-model.trim="project_name" placeholder="输入物资名称" />
    </el-form-item>
    <el-form-item>
      <el-select v-model="applyData.value">
        <el-option
          v-for="item in applyData.options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="queryApply">查询</el-button>
      <el-button @click="handelAddApply">新增</el-button>
    </el-form-item>
  </el-form>

  <!-- 分割线 -->
  <el-divider />

  <!-- 表格数据 -->
  <el-table :data="applyList" border style="width: 100%">
    <el-table-column prop="apply_time" label="申请时间" width="240" />
    <el-table-column prop="state" label="状态" width="150" />
    <el-table-column prop="apply_type" label="申请类型" width="180" />
    <el-table-column prop="project_name" label="项目名称" width="200" />
    <el-table-column prop="amount" label="采购金额" width="250" />
    <el-table-column prop="reason" label="采购原因" width="300" />
    <el-table-column label="操作" width="200">
      <el-button text @click="handelDetail">详情</el-button>
    </el-table-column>
    <template #empty>
      <el-empty description="暂无数据" />
    </template>
  </el-table>
  <div class="pagination">
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="sizes"
      @current-change="handelPages"
    />
  </div>
</template>

<script>
import { applyQuery } from "@/apis/Purchase/apply";

export default {
  data() {
    return {
      applyList: [],
      total: 0,
      project_name: "",
      sizes: 10,
      pages: 1,
      applyData: {
        options: [
          {
            value: "1",
            label: "全部",
          },
          {
            value: "2",
            label: "新建",
          },
          {
            value: "3",
            label: "部门确认",
          },
          {
            value: "4",
            label: "财务审批",
          },
          {
            value: "5",
            label: "拆单归档",
          },
          {
            value: "6",
            label: "取消订单",
          },
        ],
        value: "全部",
      },
    };
  },

  created() {
    this.findApplyList();
  },

  //查询供应商数据
  methods: {
    async findApplyList(name, size = this.sizes, page = this.pages) {
      try {
        const result = await applyQuery({ name, size, page });
        this.applyList = result.rows;
        this.total = result.count;
      } catch (error) {
        console.log("error:", error);
      }
    },

    //查询供应商信息
    queryApply() {
      this.applyList.forEach((item) => {
        if (item.project_name.includes(this.project_name)) {
          this.findApplyList(this.project_name);
        } else {
          this.findApplyList();
        }
      });
    },

    //跳转新增申请单页面
    handelAddApply() {
      this.$router.push("/purchase/apply/edit");
    },

    //跳转申请单详情页
    handelDetail() {
      this.$router.push("/purchase/apply/detail");
    },

    //页码改变，翻页查询数据
    handelPages(e) {
      this.pages = e;
      this.findApplyList();
    },
  },
};
</script>

<style lang="less" scoped>
.el-form {
  float: right;
  line-height: 50px;
  .el-form-item {
    margin: 0 10px;
  }
}
.el-divider {
  margin-top: 50px;
}
.pagination {
  width: 100%;
  height: 50px;
  position: fixed;
  left: 0;
  bottom: 50px;
  // .el-pagination {
  //   margin: 13px calc(100% / 2);
  // }
}
</style>
